package com.crm.task;

import com.crm.DTO.BusinessTaskDto;
import com.crm.service.RulePoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * 商机规则任务
 */
@Component
@Slf4j
public class BusinessTask {
    /**
     * 商机类型
     */
    private final Long BUSINESS_TYPE = 1L;

    @Autowired
    private RulePoolService rulePoolService;

    /**
     * 商机回收定时任务
     * 描述
     * 当商机超过规定时间且没有进行任何操作时，则进行回收
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void businessRecycleTask() {
        // 获取回收时限,小时
        Integer recycleTimeHours = rulePoolService.getRecycleTime(BUSINESS_TYPE);
        // 获取全部商机  和  最后一次根据记录
        ArrayList<BusinessTaskDto> businessToSea = businessTaskList(recycleTimeHours);
        //移入公海
        rulePoolService.businessToSea(businessToSea);
        // 通知
        rulePoolService.businessNotice(businessToSea);
    }


    /**
     * 商机回收定提醒
     * 描述
     * 当商机超过规定时间且没有进行任何操作时，则进行回收
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void businessNoticeTask() {
        // 获取回收时限
        Integer recycleTimeHours = rulePoolService.getRecycleTime(BUSINESS_TYPE);
        //获取提醒时限
        Integer warnTimeHours = rulePoolService.getWarnTime(BUSINESS_TYPE);
        //警告时间
        Integer noticeTime = recycleTimeHours - warnTimeHours;
        // 获取全部商机  和  最后一次根据记录
        ArrayList<BusinessTaskDto> businessToSea = businessTaskList(noticeTime);
        // 通知
        rulePoolService.businessNotice(businessToSea);
    }

    /**
     * 获取需要进行处理的商机
     *
     * @return
     */
    ArrayList<BusinessTaskDto> businessTaskList(Integer noticeTime) {
        ArrayList<BusinessTaskDto> businessTaskDtoList = rulePoolService.businessTaskList();
        if (businessTaskDtoList.isEmpty()) {
            log.info("没有数据");
            return null;
        }
        ArrayList<BusinessTaskDto> businessToSea = new ArrayList<>();
        for (BusinessTaskDto businessTaskDto : businessTaskDtoList) {
            //没有记录
            if (businessTaskDto.getLastRecordTime() == null
                    && businessTaskDto.getUpdateTime().plusHours(noticeTime).isBefore(LocalDateTime.now())) {
                //徐要移到公海商机 和通知
                BusinessTaskDto toSea = new BusinessTaskDto();
                toSea.setId(businessTaskDto.getId());
                toSea.setOwner(businessTaskDto.getOwner());
                businessToSea.add(toSea);
            }
            //有记录
            if (businessTaskDto.getLastRecordTime() != null
                    && businessTaskDto.getUpdateTime().plusHours(noticeTime).isBefore(LocalDateTime.now())) {
                // 移到公海
                BusinessTaskDto toSea = new BusinessTaskDto();
                toSea.setId(businessTaskDto.getId());
                toSea.setOwner(businessTaskDto.getOwner());
                businessToSea.add(toSea);
                //通知
            }
        }
        return businessToSea;
    }
}
